package shg.book.util
{
	import flash.display.GradientType;
	import flash.display.InterpolationMethod;
	import flash.display.Shape;
	import flash.display.SpreadMethod;
	import flash.events.Event;
	import flash.geom.Matrix;
	
	import mx.controls.HTML;
	import mx.controls.Image;
	
	import shg.book.size.BookData;
	import shg.book.view.AuthorEdit;
	import shg.book.view.ReadTextBook;

	public class DrawImg
	{
		private var coverPage_one:uint=0;
		private var coverPage_two:uint=0;
		private var coverPage_three:uint=0;
		private var coverPage_four:uint=0;
		//页码
		public var index:Number=1;
		
		static private var bgColor:uint      = 0xFFFFFC;
		static private var borderColor:uint  = 0x666666;
		static private var borderSize:uint   = 0;
		
		public function DrawImg()
		{
		}
		public function drawCenter(event:Event):void{
			var type:String=GradientType.LINEAR;//指定线性渐变
			var colors:Array=[0x000000,0xffffff];//黑色到浅灰色的过度
			var alphas:Array=[0.9,0.1];//指定colors中对应的颜色的透明值
			if(index%2==0){
				colors=[0xffffff,0x000000];//黑色到浅灰色的过度
				alphas=[0.1,0.9];//指定colors中对应的颜色的透明值
			}
			var ratios:Array=[0,255];//颜色分布比率的数组
			
			var matrix:Matrix=new Matrix();
			var width:Number=60;//渐变框的宽度
			var height:Number=20;//渐变框的高度,这个高度是任意的，可以设置为任意的一个数据
			var rotation:Number=0;//旋转量
			var tx:Number=0;// 沿 x 轴向右平移的距离
			if(index%2==0){
				tx=BookData.BOOK_WIDTH/2-width;
			}
			var ty:Number=0;// 沿 y 轴向下平移的距离
			matrix.createGradientBox(width,height,rotation,tx,ty);
			
			var spreadMethod:String=SpreadMethod.PAD;//渐变的样式
			var interpolationMethod:String=InterpolationMethod.LINEAR_RGB;//
			var focalPointRatio:Number=0;//一个控制渐变的焦点位置的数字
			
			var shape:Shape=new Shape();
			shape.graphics.beginGradientFill(type,colors,alphas,ratios,matrix,spreadMethod,interpolationMethod,focalPointRatio);
			shape.graphics.drawRect(0,0,BookData.BOOK_WIDTH/2,BookData.BOOK_HEIGTH);
			
			var drawImg:HTML=new HTML();
			drawImg.addChild(shape);
			
			event.currentTarget.addChild(drawImg);
		}
		public function drawLine(authorEdir:AuthorEdit,pageIndex:uint):void{
			var type:String=GradientType.LINEAR;//指定线性渐变
			var colors:Array=[0x000000,0xffffff];//黑色到浅灰色的过度
			var alphas:Array=[0.9,0.1];//指定colors中对应的颜色的透明值
			if(pageIndex==2 || pageIndex==4){
				colors=[0xffffff,0x000000];//黑色到浅灰色的过度
				alphas=[0.1,0.9];//指定colors中对应的颜色的透明值
			}
			var ratios:Array=[0,255];//颜色分布比率的数组
			
			var matrix:Matrix=new Matrix();
			var width:Number=100;//渐变框的宽度
			var height:Number=20;//渐变框的高度,这个高度是任意的，可以设置为任意的一个数据
			var rotation:Number=0;//旋转量
			var tx:Number=0;// 沿 x 轴向右平移的距离
			if(pageIndex==2 || pageIndex==4){
				tx=BookData.BOOK_WIDTH/2-width;
			}
			var ty:Number=0;// 沿 y 轴向下平移的距离
			matrix.createGradientBox(width,height,rotation,tx,ty);
			
			var spreadMethod:String=SpreadMethod.PAD;//渐变的样式
			var interpolationMethod:String=InterpolationMethod.LINEAR_RGB;//
			var focalPointRatio:Number=0;//一个控制渐变的焦点位置的数字
			
			var shape:Shape=new Shape();
			shape.graphics.beginGradientFill(type,colors,alphas,ratios,matrix,spreadMethod,interpolationMethod,focalPointRatio);
			shape.graphics.drawRect(0,0,BookData.BOOK_WIDTH/2,BookData.BOOK_HEIGTH);
			
			var drawImg:HTML=new HTML();
			drawImg.addChild(shape);
			if(pageIndex==1){
				if(coverPage_one==0)//获取最初的所有子类,这里在这个程序运行当中只执行一次
					coverPage_one=authorEdir.drawCaverImg_one.getChildren().length;
				
				for(;coverPage_one<authorEdir.drawCaverImg_one.getChildren().length;){
					authorEdir.drawCaverImg_one.removeChildAt(coverPage_one);
				}
				authorEdir.drawCaverImg_one.addChild(drawImg);
				drawCoverBorder(authorEdir,pageIndex);
			}else if(pageIndex==2){
				if(coverPage_two==0)//获取最初的所有子类,这里在这个程序运行当中只执行一次
					coverPage_two=authorEdir.drawCaverImg_two.getChildren().length;
				
				for(;coverPage_two<authorEdir.drawCaverImg_two.getChildren().length;){
					authorEdir.drawCaverImg_two.removeChildAt(coverPage_two);
				}
				authorEdir.drawCaverImg_two.addChild(drawImg);
				drawCover(authorEdir,pageIndex);
				
			}else if(pageIndex==3){
				if(coverPage_three==0)//获取最初的所有子类,这里在这个程序运行当中只执行一次
					coverPage_three=authorEdir.drawCaverImg_three.getChildren().length;
				
				for(;coverPage_three<authorEdir.drawCaverImg_three.getChildren().length;){
					authorEdir.drawCaverImg_three.removeChildAt(coverPage_three);
				}
				authorEdir.drawCaverImg_three.addChild(drawImg);
				drawCover(authorEdir,pageIndex);
			}else if(pageIndex==4){
				/////////////four
				if(coverPage_four==0)//获取最初的所有子类,这里在这个程序运行当中只执行一次
					coverPage_four=authorEdir.drawCaverImg_four.getChildren().length;
				
				for(;coverPage_four<authorEdir.drawCaverImg_four.getChildren().length;){
					authorEdir.drawCaverImg_four.removeChildAt(coverPage_four);
				}
				authorEdir.drawCaverImg_four.addChild(drawImg);
				drawCoverBorder(authorEdir,pageIndex);
			}
		}
		private function drawCover(authorEdir:AuthorEdit,pageIndex:uint):void{
			var type:String=GradientType.LINEAR;//指定线性渐变
			var colors:Array=[0x000000,0xffffff];//黑色到浅灰色的过度
			var alphas:Array=[0.9,0.1];//指定colors中对应的颜色的透明值
			if(pageIndex==3){
				colors=[0xffffff,0x000000];//黑色到浅灰色的过度
				alphas=[0.1,0.9];//指定colors中对应的颜色的透明值
			}
			var ratios:Array=[0,255];//颜色分布比率的数组
			
			var matrix:Matrix=new Matrix();
			var width:Number=20;//渐变框的宽度
			var height:Number=100;//渐变框的高度,这个高度是任意的，可以设置为任意的一个数据
			var rotation:Number=0;//旋转量
			var tx:Number=200;// 沿 x 轴向右平移的距离
			if(pageIndex==3)
				tx=BookData.BOOK_WIDTH/2-220;// 沿 x 轴向右平移的距离
			
			var ty:Number=0;// 沿 y 轴向下平移的距离
			matrix.createGradientBox(width,height,rotation,tx,ty);
			
			var spreadMethod:String=SpreadMethod.PAD;//渐变的样式
			var interpolationMethod:String=InterpolationMethod.LINEAR_RGB;
			var focalPointRatio:Number=0;//一个控制渐变的焦点位置的数字
			
			var shape:Shape=new Shape();
			shape.graphics.beginGradientFill(type,colors,alphas,ratios,matrix,spreadMethod,interpolationMethod,focalPointRatio);
			if(pageIndex==2){
				shape.graphics.drawRect(tx+5,0,20,BookData.BOOK_HEIGTH);
				
			}else if(pageIndex==3){
				shape.graphics.drawRect(tx-4,0,20,BookData.BOOK_HEIGTH);
				
			}
			var drawImgTwo:HTML=new HTML();
			drawImgTwo.addChild(shape);
			if(pageIndex==2){
				authorEdir.drawCaverImg_two.addChild(drawImgTwo);
				
				drawCoverBorder(authorEdir,pageIndex);
			}else if(pageIndex==3){
				authorEdir.drawCaverImg_three.addChild(drawImgTwo);
				
				drawCoverBorder(authorEdir,pageIndex);
			}
		}
		
		private function drawCoverBorder(authorEdir:AuthorEdit,pageIndex:uint):void{
			var type:String=GradientType.LINEAR;//指定线性渐变
			var colors:Array=[0x000000,0xffffff];//黑色到浅灰色的过度
			var alphas:Array=[0.3,0.1];//指定colors中对应的颜色的透明值
			var ratios:Array=[0,255];//颜色分布比率的数组
			if(pageIndex==1 || pageIndex==3){
				 colors=[0xffffff,0x000000];//黑色到浅灰色的过度
				 alphas=[0.1,0.3];//指定colors中对应的颜色的透明值
			}
			
			var matrix:Matrix=new Matrix();
			var width:Number=20;//渐变框的宽度
			var height:Number=100;//渐变框的高度,这个高度是任意的，可以设置为任意的一个数据
			var rotation:Number=0;//旋转量
			var tx:Number=0;// 沿 x 轴向右平移的距离
			var ty:Number=0;// 沿 y 轴向下平移的距离
			if(pageIndex==1|| pageIndex==3){
				tx=BookData.BOOK_WIDTH/2-17;
			}
			matrix.createGradientBox(width,height,rotation,tx,ty);
			
			var spreadMethod:String=SpreadMethod.PAD;//渐变的样式
			var interpolationMethod:String=InterpolationMethod.LINEAR_RGB;
			var focalPointRatio:Number=0;//一个控制渐变的焦点位置的数字
			
			var shape:Shape=new Shape();
			shape.graphics.beginGradientFill(type,colors,alphas,ratios,matrix,spreadMethod,interpolationMethod,focalPointRatio);
			shape.graphics.drawRect(0,0,BookData.BOOK_WIDTH/2,BookData.BOOK_HEIGTH);
			
			var drawImgTwo:HTML=new HTML();
			drawImgTwo.addChild(shape);
			if(pageIndex==1){
				authorEdir.drawCaverImg_one.addChild(drawImgTwo);
			}else if(pageIndex==2){
				authorEdir.drawCaverImg_two.addChild(drawImgTwo);
			}else if(pageIndex==3){
				authorEdir.drawCaverImg_three.addChild(drawImgTwo);
			}else if(pageIndex==4){
				authorEdir.drawCaverImg_four.addChild(drawImgTwo);
			}
		}
		/**
		 * 当启动一本书籍的时候首次为本书籍添加边框
		 * 只要将边框添加到屏幕的右边
		 * */
		public static function drawBorderFirst(_x:Number,_height:Number,borderWidth:Number,borderImg:Image):void{
			var child:Shape = new Shape();
			var img:Image=new Image();
			child.graphics.beginFill(bgColor);
			child.graphics.lineStyle(borderSize,borderColor,0.0);
			child.graphics.drawRect(_x, 0, borderWidth, _height);
			child.graphics.endFill();
			img.addChild(child);
			for(var i:int=_x;i<(_x+borderWidth);i=i+2){
				child.graphics.beginFill(bgColor);
				child.graphics.lineStyle(borderSize, borderColor,0.5);
				child.graphics.drawRect(i, 0, 0.5, _height);
				child.graphics.endFill();
				img.addChild(child);
			}
			//首先删除原来的图像，然后才添加新的图形进去
			try{
				borderImg.removeChildAt(0);
			}catch(error:Error){
				trace(error.toString());
			}
			borderImg.addChild(img);
		}
		
	}
}